
_ = require 'underscore'
fsp = require '../bin/libs/fs-promises'
Q = require 'q'
path = require 'path'
CustomApp = require '../db/CustomApp'
#图片上传模块


module.exports = (app) ->

    #图标上传 依赖app_id
    app.post '/apps/image/logo/:app_id', app.uploader.single('logo'), (req, res, next) ->
      console.log "上传图片: "
      console.log req.file
      app_id = req.params.app_id
      filename = req.file.filename + req.file.originalname.substring(req.file.originalname.lastIndexOf('.'))
      uploadedPath = req.file.path
      dest_path = path.join app.get('sandbox_path'), app_id, 'images', 'icons', filename
      dest_dir = path.join app.get('sandbox_path'), app_id, 'images', 'icons'
      download_url = "/apps/image/logo/#{app_id}/#{filename}"
      #多图同时上传
      fsp.rename uploadedPath, dest_path
      .then ->
        fsp.traverse_safe dest_dir
      .then (result) ->
        logo = _.map result, (each) ->
          return   "/apps/image/logo/#{app_id}" + each.substring(each.lastIndexOf('/'))
        CustomApp.findOneAndUpdate _id: app_id, {logo: logo}, (err, data) ->
           return res.status(404).json {code:-1, message:'upload Failed'} if err
           res.status(200).json {code: 0, data: download_url}
      .catch (e) ->
        console.log e
        res.status(404).json {code: -1, message: 'upload Failed no app_id'}


    #图标下载 by app_id
    app.get '/apps/image/logo/:app_id/:image_id', (req, res, next) ->
      file_path = path.join app.get('sandbox_path'), req.params.app_id, 'images', 'icons', req.params.image_id
      res.sendFile file_path, (err) ->
        return res.status(404).json {code:-1, message:'Can\'t find file.'} if err
        console.log "sented #{req.params.image_id}"

    #图标删除 by app_id
    app.delete '/apps/image/logo/:app_id/:image_id', (req, res, next) ->
      file_path = path.join app.get('sandbox_path'), req.params.app_id, 'images', 'icons', req.params.image_id
      download_url = "/apps/image/logo/#{req.params.app_id}/#{req.params.image_id}"
      fsp.remove_safe file_path
      .then ->
        CustomApp.findOne _id: req.params.app_id, (err, obj) ->
          return res.status(404).json {code:-1, message:'delete Failed'} if err
          del_img = _.indexOf obj.logo, download_url
          obj.logo.splice del_img, 1 if del_img != -1
          CustomApp.findOneAndUpdate _id: req.params.app_id, {logo: obj.logo}, (err, data) ->
             return res.status(404).json {code:-1, message:'delete Failed'} if err
             res.status(200).json {code: 0, message: 'delete successfully'}


    #启动图上传 依赖app_id
    app.post '/apps/image/splash/:app_id', app.uploader.single('splash'), (req, res, next) ->
      console.log "上传图片: "
      app_id = req.params.app_id
      filename = req.file.filename + req.file.originalname.substring(req.file.originalname.lastIndexOf('.'))
      uploadedPath = req.file.path
      dest_path = path.join app.get('sandbox_path'), app_id, 'images', 'launchImages', filename
      dest_dir = path.join app.get('sandbox_path'), app_id, 'images', 'launchImages'
      download_url = "/apps/image/splash/#{app_id}/#{filename}"

      fsp.rename uploadedPath, dest_path
      .then ->
        fsp.traverse_safe dest_dir
      .then (result) ->
        splash = _.map result, (each) ->
          return   "/apps/image/splash/#{app_id}" + each.substring(each.lastIndexOf('/'))

        CustomApp.findOneAndUpdate _id: app_id, {splash: splash}, (err, data) ->
           return res.status(404).json {code:-1, message:'upload Failed'} if err
           res.status(200).json {code: 0, data: download_url}
      .catch (e) ->
        console.log e
        res.status(404).json {code: -1, message: 'upload Failed no app_id'}



    #启动图下载 by app_id
    app.get '/apps/image/splash/:app_id/:image_id', (req, res, next) ->
      file_path = path.join app.get('sandbox_path'), req.params.app_id, 'images', 'launchImages', req.params.image_id
      res.sendFile file_path, (err) ->
        return res.status(404).json {code:-1, message:'Can\'t find file.'} if err
        console.log "sented #{req.params.image_id}"


    #启动图删除 by app_id
    app.delete '/apps/image/splash/:app_id/:image_id', (req, res, next) ->
      file_path = path.join app.get('sandbox_path'), req.params.app_id, 'images', 'launchImages', req.params.image_id
      download_url = "/apps/image/splash/#{req.params.app_id}/#{req.params.image_id}"
      fsp.remove_safe file_path
      .then ->
        CustomApp.findOne _id: req.params.app_id, (err, obj) ->
          return res.status(404).json {code:-1, message:'delete Failed'} if err
          del_img = _.indexOf obj.splash, download_url
          obj.splash.splice del_img, 1 if del_img != -1
          CustomApp.findOneAndUpdate _id: req.params.app_id, {splash: obj.splash}, (err, data) ->
             return res.status(404).json {code:-1, message:'delete Failed'} if err
             res.status(200).json {code: 0, message: 'delete successfully'}
